﻿using ArtMath.Component.CoordinateSystem;
using ArtMath.Core.Data;
using ArtMath.Resource;
using System;
using System.ComponentModel;

namespace ArtMath.Data
{
    [Serializable]
    public class PPointS : RPointS//, IFormatPainter<PPointS>
    {
        PadStringX rou, theta;
        public PPointS(SketchPad pad, string rou = null, string theta = null) :
            base(pad, 
                string.IsNullOrEmpty(rou) ? "0" : string.IsNullOrEmpty(theta) ? rou : (double.Parse(rou) * Math.Cos(pad.GetRad(double.Parse(theta)))).ToString(),
                string.IsNullOrEmpty(rou) || string.IsNullOrEmpty(theta) ? "0" : (double.Parse(rou) * Math.Sin(pad.GetRad(double.Parse(theta)))).ToString())
        {
            ρ = new PadStringX(pad, rou);
            θ = new PadStringX(pad, theta);
        }
        public PadStringX ρ
        {
            get { return rou; }
            set
            {
                if (rou is object)
                {
                    rou.ParametersValueChanged -= RT_ParametersValueChanged;
                    rou.ExpressionSet -= RT_ExpressionSet;
                }
                rou = value;
                if (rou is object)
                {
                    rou.ParametersValueChanged += RT_ParametersValueChanged;
                    rou.ExpressionSet += RT_ExpressionSet;
                }
            }
        }
        public PadStringX θ
        {
            get { return theta; }
            set
            {
                if (theta is object)
                {
                    theta.ParametersValueChanged -= RT_ParametersValueChanged;
                    theta.ExpressionSet -= RT_ExpressionSet;
                }
                theta = value;
                if (theta is object)
                {
                    theta.ParametersValueChanged += RT_ParametersValueChanged;
                    theta.ExpressionSet += RT_ExpressionSet;
                }
            }
        }
        public PointD ToPPointD()
        {
            return new PointD(ρ.Value, θ.Value);
        }
        public override PointD ToMathCoordinate()
        {
            return ToPPointD();
        }
        void RT_ParametersValueChanged(DocStringX sender, ParametersValueChangedEventArgs e)
        {
            rtEvent();
        }
        void RT_ExpressionSet(StringX sender, ExpressionSetEventArgs e)
        {
            rtEvent();
        }
        void rtEvent()
        {
            PolarCoordinate pc = ρ.LinkedPad.Coordinate as PolarCoordinate;
            if (pc != null)
            {
                PointD xy = pc.RTtoXY(ToPPointD());
                X.SetExpression(xy.X);
                Y.SetExpression(xy.Y);
            }
        }
        public override void SetSeparately(double rou, double theta)
        {
            ρ.SetExpression(rou);
            θ.SetExpression(theta);
        }
        [ReadOnly(true)]
        public override PadStringX X
        {
            get
            {
                return base.X;
            }

            set
            {
                base.X = value;
            }
        }
        [ReadOnly(true)]
        public override PadStringX Y
        {
            get
            {
                return base.Y;
            }

            set
            {
                base.Y = value;
            }
        }
        public override void SetFromString(string exp)
        {
            string[] tokens = exp.Split(',', '，');
            if (tokens.Length == 2)
            {
                ρ.SetExpression(tokens[0].Trim());
                θ.SetExpression(tokens[1].Trim());
            }
            else
            {
                throw new ArgumentException(string.Format(Resources.Translate("AcceptNArgs"), Resources.Translate("NumberPair"), 2));
            }
        }
        public override string ToString()
        {
            return ρ.ToString() + ", " + θ.ToString();
        }

        //public void UseFormat(PPointS obj)
        //{
        //    ρ.UseFormat(obj.ρ);
        //    θ.UseFormat(obj.θ);
        //}
    }
}
